package com.android.camera.animation;

import android.util.Log;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

public class AnimationMeteringIn extends Animation {

    public static final int DURATION_DEFAULT = 300;
    public static final int DURATION_SHORT = 100;
    public static final int DURATION_LONG = 500;

    private float mScaleX, mScaleY;
    private float mAlpha = 1.0f;

    public AnimationMeteringIn(View view) {
        mView = view;
        mInterpolator = new AccelerateDecelerateInterpolator();
        mListener = null;
    }

    public void animate() {
        mView.setScaleX(mScaleX);
        mView.setScaleY(mScaleY);
        mView.setAlpha(mAlpha);

        AnimatorSet scaleSet = new AnimatorSet();
        scaleSet.playTogether(ObjectAnimator.ofFloat(mView, View.SCALE_X, 1.0f),
                ObjectAnimator.ofFloat(mView, View.SCALE_Y, 1.0f),
                ObjectAnimator.ofFloat(mView, View.ALPHA, 1.0f));
        scaleSet.setInterpolator(mInterpolator);
        scaleSet.setDuration(mDuration);
        scaleSet.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationStart(Animator animation) {
                mView.setVisibility(View.VISIBLE);
                if (getListener() != null) {
                    getListener().onAnimationStart(AnimationMeteringIn.this);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (getListener() != null) {
                    getListener().onAnimationEnd(AnimationMeteringIn.this);
                }
            }
        });
        scaleSet.setStartDelay(mDelay);
        scaleSet.start();
    }

    public TimeInterpolator getInterpolator() {
        return mInterpolator;
    }

    public AnimationMeteringIn setInterpolator(TimeInterpolator interpolator) {
        mInterpolator = interpolator;
        return this;
    }

    public long getDuration() {
        return mDuration;
    }

    public AnimationMeteringIn setDuration(long duration) {
        mDuration = duration;
        return this;
    }

    public long getDelay() {
        return mDelay;
    }

    public AnimationMeteringIn setDelay(int delay) {
        mDelay = delay;
        return this;
    }

    public AnimationListener getListener() {
        return mListener;
    }

    public AnimationMeteringIn setListener(AnimationListener listener) {
        mListener = listener;
        return this;
    }

    public AnimationMeteringIn setScale(float scaleX, float scaleY) {
        mScaleX = scaleX;
        mScaleY = scaleY;
        return this;
    }

    public AnimationMeteringIn setAlpha(float alpha) {
        mAlpha = alpha;
        return this;
    }
}
